#include "../include/Segment.h"

Segment::Segment(int id)
{
	//Set ID
	m_id = id;
	m_sectionId = 0;
	//Initialize singular values
	m_loudnessBegin = 0.0;
	m_loudnessMax = 0.0;
	m_loudnessEnd = 0.0;
	m_timeLoudnessMax = 0.0;
	m_duration = -1.0;
	
	//Declare space to hold the array of pitch & timbre data
	m_pitches = (float*)malloc(sizeof(float) * NUM_VOICES);
	m_timbres = (float*)malloc(sizeof(float) * NUM_VOICES);
	m_tatums = NULL;
	m_numTatums = 0;
	
	//Initialize memory contents to 0
	memset(m_pitches, 0, sizeof(float) * NUM_VOICES);
	memset(m_timbres, 0, sizeof(float) * NUM_VOICES);
}

Segment::~Segment()
{
	//Get rid of the arrays
	free(m_pitches);
	free(m_timbres);
}

void Segment::setStart(float start)
{
	m_start = start;
}

void Segment::setDuration(float duration)
{
	m_duration = duration;
}

void Segment::setLoudnessBegin(float f)
{
	m_loudnessBegin = f;
}

void Segment::setLoudnessMax(float f)
{
	m_loudnessMax = f;
}

void Segment::setLoudnessEnd(float f)
{
	m_loudnessEnd = f;
}

void Segment::setTimeLoudnessMax(float f)
{
	m_timeLoudnessMax = f;
}

void Segment::setPitch(int i, float val)
{
	m_pitches[i] = val;
}

void Segment::setTimbre(int i, float val)
{
	m_timbres[i] = val;
}

int Segment::getId()
{
	return m_id;
}

float Segment::getStart()
{
	return m_start;
}

float Segment::getDuration()
{
	return m_duration;
}

float Segment::getLoudnessBegin()
{
	return m_loudnessBegin;
}

float Segment::getLoudnessMax()
{
	return m_loudnessMax;
}

float Segment::getLoudnessEnd()
{
	return m_loudnessEnd;
}

float Segment::getTimeLoudnessMax()
{
	return m_timeLoudnessMax;
}

float Segment::getPitch(int i)
{
	return m_pitches[i];
}

float Segment::getTimbre(int i)
{
	return m_timbres[i];
}

void Segment::print()
{
	printf("############################Segment ID #%d###########################\n", m_id);
	
	printf("Start: %f\nDuration: %f\nLoudness Begin: %f\nLoudness Max: %f\nLoudness End: %f\nTime Loudness Max: %f\nNo. of Tatums: %d\n", m_start, m_duration, m_loudnessBegin, m_loudnessMax, m_loudnessEnd, m_timeLoudnessMax, m_numTatums);
	
	printf("Tatums:\n ");
	
	int i;
	for(i = 0; i < m_numTatums; i++)
	{
		printf("\t%d. %f\n", (i + 1), m_tatums[i]);
	}
	printf("\n");
	
	printf("Pitches: ");
	
	for(i = 0; i < 12; i++)
	{
		printf("%f ", m_pitches[i]);
	}
	printf("\n");
	
	printf("Timbre Coeffs: ");
	
	for(i = 0; i < 12; i++)
	{
		printf("%f ", m_timbres[i]);
	}
	printf("\n");

}

bool Segment::processTatums(float* tatums, int numTatums)
{
	//
	if(m_duration == -1.0)
	{
		return false;
	}
	assert(tatums);
	std::vector<float> relevantTatums;
	///printf("Start: %f, Duration: %f\n", m_start, m_duration);
	for(int i = 0; i < numTatums; i++)
	{
		//TODO: Maybe put in epsilon so we're not doing direct float comparison...or not...
		if( (tatums[i] >= m_start) && (tatums[i] <= (m_start + m_duration)) )
		{
			//Relevant tatum!
			//printf("Pushed! -- %f\n", tatums[i]);
			relevantTatums.push_back(tatums[i]);
		}		
	}
	
	//Allocate memory for relevant tatums
	m_tatums = (float*)malloc(sizeof(float) * relevantTatums.size());
	memset(m_tatums, 0, relevantTatums.size());
	
	for(unsigned int i = 0; i < relevantTatums.size(); i++)
	{
		m_tatums[i] = relevantTatums[i];
	}
	//printf("Num Tatums: %d\n", relevantTatums.size());
	m_numTatums = relevantTatums.size();
	
	return true;
}

float* Segment::getTatums()
{
	return m_tatums;
}

int Segment::getNumTatums()
{
	return m_numTatums;
}

float Segment::getTatum(int i)
{
	return m_tatums[i];
}

int Segment::getSectionId()
{
	return m_sectionId;
}

void Segment::setSectionId(int i)
{
	m_sectionId = i;
}
